package com.hnucm.cooklife.presenter.me;

import androidx.annotation.NonNull;


import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.bean.me.DishColl;
import com.hnucm.cooklife.model.MeDishCollModel;


import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MeDishCollPresenter extends BasePresenter<com.hnucm.cooklife.contract.meDishColl.Contract.IMeDishCollView> implements com.hnucm.cooklife.contract.meDishColl.Contract.IMeDishCollPresenter {
    com.hnucm.cooklife.contract.meDishColl.Contract.IMeDishCollModel mIMeDishCollModel;

    public MeDishCollPresenter() {
        mIMeDishCollModel = new MeDishCollModel();
    }

    @Override
    public void loadMeDishColl(int userId) {
        mIMeDishCollModel.loadMeDishColl(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<DishColl>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull List<DishColl> DishCollList) {

                        if(isViewAttached()){
                            getView().onLoadMeDishColl(DishCollList);
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
//
                        System.out.println(e.getMessage());
//                        System.out.println("tesryt");
                        if(isViewAttached()){
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void refreshMeDishColl(int userId) {

    }
}
